package cn.jari.erp


import com.csicit.ace.jpaleaf.repository.EnhancedJpaRepository
import liquibase.integration.spring.SpringLiquibase
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.core.io.DefaultResourceLoader
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.data.jpa.repository.support.AceJpaRepositoryFactoryBean
import org.springframework.orm.jpa.JpaTransactionManager
import org.springframework.orm.jpa.JpaVendorAdapter
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
import org.springframework.transaction.PlatformTransactionManager
import javax.persistence.EntityManagerFactory
import javax.sql.DataSource
/*
 * 警告：代码由鲲舟平台生成，请勿手动修改。
 * Warn: This class file is generated by JARI-ACE platform, please do not modify manually。
 */

private const val JARI_ERP_PRIMARY_DATASOURCE = "jari-erp-primary-ds"
private const val JARI_ERP_PRIMARY_EMF = "jari-erp-primary-emf"
private const val JARI_ERP_PRIMARY_TM = "jari-erp-primary-tm"

/**
 * 将所有子服务的主数据源合成一个主数据源
 *
 */
@EnableJpaRepositories(
    basePackages = [
        "com.csicit.ace.jpaleaf.repository",
        "com.csicit.ace.app.service.repositories",
        "com.csicit.ace.audit.service.repository",
        "com.csicit.ace.domain.modeler.repositories",
        "com.csicit.ace.files.repository",
        "com.csicit.ace.push.repository",
        "com.csicit.ace.patcher.repository",
        "cn.jari.erp.inventory.repositories.primary"],
    repositoryFactoryBeanClass = AceJpaRepositoryFactoryBean::class,
    repositoryBaseClass = EnhancedJpaRepository::class,
    entityManagerFactoryRef = JARI_ERP_PRIMARY_EMF,
    transactionManagerRef = JARI_ERP_PRIMARY_TM
)
@Configuration
class JariErpPrimaryDataSourceConfiguration {

    @Bean
    @ConfigurationProperties("spring.datasource.jari-erp-primary-ds")
    fun jariErpDataSourceProperties(): DataSourceProperties {
        return DataSourceProperties()
    }

    @Primary
    @Bean(name = [JARI_ERP_PRIMARY_DATASOURCE])
    fun jariErpDatasource(): DataSource {
        return jariErpDataSourceProperties().initializeDataSourceBuilder().build()
    }

    @Primary
    @Bean(name = [JARI_ERP_PRIMARY_EMF, "entityManagerFactory"])
    fun jariErpEntityManagerFactory(
        @Qualifier(JARI_ERP_PRIMARY_DATASOURCE) dataSource: DataSource?,
        builder: EntityManagerFactoryBuilder,
        jpaVendorAdapter: JpaVendorAdapter
    ): LocalContainerEntityManagerFactoryBean {
        val jpaProperties: MutableMap<String, Any?> = HashMap()
        jpaProperties["hibernate.physical_naming_strategy"] =
            "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy"
        jpaProperties["hibernate.implicit_naming_strategy"] =
            "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"
        for (entry in jpaVendorAdapter.jpaPropertyMap.entries) {
            jpaProperties[entry.key] = entry.value
        }
        return builder
            .dataSource(dataSource)
            .packages(
                "com.csicit.ace.app.service.model",
                "com.csicit.ace.audit.service.model",
                "com.csicit.ace.domain.modeler.model",
                "com.csicit.ace.files.model",
                "com.csicit.ace.push.model",
                "com.csicit.ace.patcher.entity",
                "com.csicit.ace.axon",
                "org.axonframework.eventsourcing.eventstore.jpa",
                "org.axonframework.eventhandling.tokenstore.jpa",
                "org.axonframework.modelling.saga.repository.jpa",
                "cn.jari.erp.inventory.entities.primary"
            )
            .properties(jpaProperties)
            .persistenceUnit("default")
            .build()
    }

    @Primary
    @Bean(name = [JARI_ERP_PRIMARY_TM, "platformTransactionManager", "transactionManager"])
    fun jariErpTransactionManager(
        @Qualifier(JARI_ERP_PRIMARY_EMF) entityManagerFactory: EntityManagerFactory
    ): PlatformTransactionManager {
        return JpaTransactionManager(entityManagerFactory)
    }

    @Bean
    fun inventoryServiceAppPrimaryLiquibase(
        @Qualifier(JARI_ERP_PRIMARY_DATASOURCE) dataSource: DataSource?
    ): SpringLiquibase {
        val liquibase = SpringLiquibase()
        // 用户模块Liquibase文件路径
        liquibase.changeLog = "classpath:db/changelog/inventory-service/primary/change-log-master.xml";
        liquibase.dataSource = dataSource
        liquibase.setShouldRun(true)
        liquibase.resourceLoader = DefaultResourceLoader()
        return liquibase
    }

}